const express = require("express")
var cors = require('cors')
require("./connect")

let {
    Bookcover
} = require("./mod/bookcover")
let {
    Chapter
} = require("./mod/chapter")

//创建网站服务器
const app = express()

app.use(cors())

app.get("/allbook", async (req, res) => {
    let booklist = await Bookcover.find()
    res.status(200).json({
        data: booklist
    })
})

app.get("/book/chapters", async (req, res) => {
    let {
        book_title
    } = req.query
    book_title = new RegExp(book_title, "i") //i 忽略大小写
    let chapterlist = await Chapter.find({
        $or: [{
                id_: book_title //没有生效
            },
            {
                book_title: {
                    $regex: book_title
                }
            }
        ]
    }).sort({
        chapter_pushtime: 1
    })
  
    res.status(200).json({
        data: chapterlist
    })
})

app.listen(3000, () => {
    console.log("服务器已经开启")
})